//
//  SQHiddenDangerViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/6.
//

#import "SQHiddenDangerViewController.h"


#import "PickerSelectView.h"
#import "LCImageChoose.h"

#import "NSString+NCDate.h"

#import "SQHierarchyModel.h"
#import "SQHiddenDangerModel.h"
#import "SQVoiceView.h"

#import "SQShowImage.h"


@interface SQHiddenDangerViewController ()<BaseTableVCInterface>

/**录音*/
@property (strong, nonatomic) SQVoiceView *voiceView;
/**语音view*/
@property (weak, nonatomic) IBOutlet UIView *view_voice;

@property (strong, nonatomic) SQHierarchyModel *hierarchyModel;

@property (weak, nonatomic) IBOutlet UITextField *tf_area;//区域
@property (weak, nonatomic) IBOutlet UITextField *tf_hierarchy;//层级
@property (weak, nonatomic) IBOutlet UITextField *tf_people;//责任人
@property (weak, nonatomic) IBOutlet UITextView *tv_content;//隐患描述
/**图片 删除图片*/
@property (weak, nonatomic) IBOutlet UIImageView *img_picOne;
@property (weak, nonatomic) IBOutlet UIImageView *img_picOneDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwoDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThree;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThreeDel;

/**隐患上报照片 三张*/
@property (strong, nonatomic) NSString *hd_report_pic_one;
@property (strong, nonatomic) NSString *hd_report_pic_two;
@property (strong, nonatomic) NSString *hd_report_pic_three;
/**隐患上报语音*/
@property (strong, nonatomic) NSString *hd_report_pic_audio;

/**上报人id(hd_report_person_id),上报人名字(hd_report_person_text),上报人部门id(hd_report_department_id),上报人部门中文(hd_report_department_text)*/
@property (strong, nonatomic) NSString *hd_confirm_department_id;
@property (strong, nonatomic) NSString *hd_confirm_person_id;
@property (strong, nonatomic) NSString *hd_confirm_person_text;


@end

@implementation SQHiddenDangerViewController{
    NSArray *imageArr;
}

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"独立隐患上报";
    
    self.hd_report_pic_one = @"";
    self.hd_report_pic_two = @"";
    self.hd_report_pic_three = @"";
    self.hd_report_pic_audio = @"";
    
    self.navigationItem.leftBarButtonItem = nil;
   
    self.voiceView = [[SQVoiceView alloc]init];
    [self.voiceView showWithChooseBlock:^(NSString * _Nonnull path) {
        self.hd_report_pic_audio = path;

    }];
    [self.view_voice  addSubview:self.voiceView];
   
    [self getHierarchy];

}


-(void)getHierarchy{
    NSString * os_level;
    
        if ([MLevel isEqualToString:@"1"] ) {
            os_level = MOne;
        }else if ( [MLevel isEqualToString:@"2"] || [MLevel isEqualToString:@"3"]){
            os_level = MTwo;
        }else if ( [MLevel isEqualToString:@"4"] ){
            os_level = MThree;
        }
    
    
    [SQHierarchyModel getDataWithParams:@{@"m_enterprise_id":MEnterpriseId,@"m_level":MLevel,@"os_level":os_level} WithCallBack:^(SQHierarchyModel * data) {
        self.hierarchyModel = data;
    }];
}


-(NSInteger)rowsCount:(NSInteger)section{
    return 0;
}


-(NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQHiddenDangerTableViewCell"]];
}

//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

/**隐患确认层级*/
- (IBAction)clickHierarchy:(UIButton *)sender {
    [self.view endEditing:YES];
   
    if (self.hierarchyModel == nil) {return;}
    
    self.hierarchyModel.nameArr.pickerSigleShow(self.tf_hierarchy,@"隐患确认层级");
    
    
    
}


/**隐婚确认负责人*/
- (IBAction)clickPeople:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self.tf_hierarchy.text isEqualToString:@""]) {
        [CCToast showMessage:@"请选择隐患确认层级"];
        return;
    }
    if (self.hierarchyModel.data[self.tf_hierarchy.selectIndex[0].intValue].nameArr.count == 0) {return;}
    self.hierarchyModel.data[self.tf_hierarchy.selectIndex[0].intValue].nameArr.pickerSigleShow(self.tf_people,@"隐患确认负责人").finish = ^(UITextField * textField){
        SQHierarchyModelArray * data = self.hierarchyModel.data[self.tf_hierarchy.selectIndex[0].intValue];
        SQHierarchyModelPersonnelArray * personnelData = data.personnel[self.tf_people.selectIndex[0].intValue];
        self.hd_confirm_department_id = data.os_id;
        self.hd_confirm_person_id = personnelData.m_id;
        self.hd_confirm_person_text = personnelData.m_name;
              
    };;

}




/**确认*/
- (IBAction)clickConfirm:(UIButton *)sender {
    /**法用途： 隐患上报和独立隐患上报提交或修改。传参：
     隐患id(hd_id),
     一级单元(hd_ipm_one),
     二级单元(hd_ipm_two),
     三级单元(hd_ipm_three),
     风险点id(hd_ipm),
     风险点中文(hd_ipm_text),
     子风险点id(hd_rt),
     子风险点中文(hd_rt_text),
     危险源id(hd_rp),
     危险源中文(hd_rp_text),
     风险类别(hd_risktype)0作业风险 1设备风险（已粘贴二维码都是设备-三发电） 2区域风险 3管理风险 4独立风险,
     场所区域(site_area),0扫码巡检隐患上报 1独立隐患上报 2专项巡检隐患上报 3隐患登记 4作业检查隐患上报(hd_type),
     上报人层级(m_level),上报人id(hd_report_person_id),上报人名字(hd_report_person_text),上报人部门id(hd_report_department_id),上报人部门中文(hd_report_department_text),组织架构确认部门id(hd_confirm_department_id),确认人员id(hd_confirm_person_id),确认人员中文(hd_confirm_person_text),隐患上报第一张照片(hd_report_pic_one),第二张照片(hd_report_pic_two),第三张照片(hd_report_pic_three),隐患上报音频(hd_report_pic_audio),隐患描述(hd_danger_description),隐患上报企业id(hd_enterprise)。成功回参：code：200|隐患增加后返回的id。失败回参：code：0|msg。*/
    [self.view endEditing:YES];
   
    if ([self verification]) {
        [SQHiddenDangerModel getDataWithParams:@{@"hd_report_pic_one":self.hd_report_pic_one?self.hd_report_pic_one:@"",
                                                 @"hd_report_pic_two":self.hd_report_pic_two?self.hd_report_pic_two:@"",
                                                 @"hd_report_pic_three":self.hd_report_pic_three?self.hd_report_pic_three:@"",
                                                 @"hd_id":@"0",
                                                 @"hd_ipm_one":@"",
                                                 @"hd_ipm_two":@"",
                                                 @"hd_ipm_three":@"",
                                                 @"hd_ipm":@"0",
                                                 @"hd_ipm_text":@"",
                                                 @"hd_rt":@"0",
                                                 @"hd_rt_text":@"",
                                                 @"hd_rp":@"0",
                                                 @"hd_rp_text":@"",
                                                 @"hd_risktype":@"4",
                                                 @"site_area":self.tf_area.text,
                                                 @"hd_type":@"1",
                                                 @"m_level":MLevel,
                                                 
                                                 @"hd_report_person_id":UID,
                                                 @"hd_report_person_text":MName,
                                                 
                                                 @"hd_report_department_id":MGroup,
                                                 @"hd_report_department_text":MNumber,
                                                 
                                                 @"hd_confirm_department_id":self.hd_confirm_department_id,
                                                 
                                                 @"hd_confirm_person_id":self.hd_confirm_person_id,
                                                 @"hd_confirm_person_text":self.hd_confirm_person_text,
                                                 
                                                @"hd_report_pic_audio":self.hd_report_pic_audio?self.hd_report_pic_audio:@"",
                                                 @"hd_danger_description":self.tv_content.text,
                                                 @"hd_enterprise":MEnterpriseId,
                                                 @"hd_enterprise_text":MEnterpriseText

        } WithCallBack:^(SQHiddenDangerModel * data) {
            [self.voiceView deleteVoiceData];
            [CCToast showMessage:@"提交成功"];
            self.tf_area.text = @"";
            self.tf_hierarchy.text = @"";
            self.tf_people.text = @"";
            self.tv_content.text = @"";
            self.hd_report_pic_one = @"";
            self.hd_report_pic_two = @"";
            self.hd_report_pic_three = @"";
            self.hd_report_pic_audio = @"";
            self.img_picOne.image = [UIImage imageNamed:@"addImage"];
           self.img_picOneDel.image = [UIImage imageNamed:@""];
            self.img_picTwoDel.image = [UIImage imageNamed:@""];
            self.img_picTwo.image = [UIImage imageNamed:@"addImage"];
            self.img_picThreeDel.image = [UIImage imageNamed:@""];
           
           self.img_picThree.image = [UIImage imageNamed:@"addImage"];
        }];
    }
}

#pragma mark ---- 取消  清空数据返回首页

- (IBAction)clickCancel:(UIButton *)sender {
    [self.voiceView deleteVoiceData];
        self.tf_area.text = @"";
        self.tf_hierarchy.text = @"";
        self.tf_people.text = @"";
        self.tv_content.text = @"";
    self.hd_report_pic_one = @"";
    self.hd_report_pic_two = @"";
    self.hd_report_pic_three = @"";
        self.hd_report_pic_audio = @"";
        self.img_picOne.image = [UIImage imageNamed:@"addImage"];
        self.img_picOneDel.image = [UIImage imageNamed:@""];
        self.img_picTwoDel.image = [UIImage imageNamed:@""];
        self.img_picTwo.image = [UIImage imageNamed:@"addImage"];
        self.img_picThreeDel.image = [UIImage imageNamed:@""];
        self.img_picThree.image = [UIImage imageNamed:@"addImage"];
       
        [self.tabBarController setSelectedIndex:0];
    
}


#pragma mark ---- 验证
- (BOOL)verification {
    BOOL ret = NO;
    if([self.tf_area.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入所在地区"];
        return NO;
    }
    
    
    if([self.tf_hierarchy.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择层级"];
        return NO;
    }
    if([self.tf_people.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请选择责任人"];
        return NO;
    }
    if([self.tv_content.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请填写隐患描述"];
        return NO;
    }
    if (![self haveStr:@[self.hd_report_pic_one,self.hd_report_pic_two,self.hd_report_pic_three,self.hd_report_pic_audio]]) {
        
        [CCToast showMessage:@"请上传照片或者音频"];
        return NO;
        
    }else{
        ret = YES;
    }
    return YES;
}

- (BOOL)haveStr:(NSArray<NSString *> *)strs{
    for (NSString *key in strs) {
        if (key.length > 0) {
            return YES;
        }
    }
    return NO;
}

     
#pragma mark ---- 图片上传
- (IBAction)clickPicOne:(id)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_pic_one]]) {
        [SQShowImage showImage:self.img_picOne];
        
    }else{
        [LCImageChoose shareCameraWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picOne.image = data[0];
            self.hd_report_pic_one =   [UIImageJPEGRepresentation(self.img_picOne.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
          
            self.img_picOneDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
}

- (IBAction)clickDelPicOne:(id)sender {
    self.img_picOne.image = [UIImage imageNamed:@"addImage"];
    self.img_picOneDel.image = [UIImage imageNamed:@""];
    self.hd_report_pic_one = @"";
}


- (IBAction)clickPicTwo:(id)sender {
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_report_pic_two]]) {
        [SQShowImage showImage:self.img_picTwo];
        
    }else{
        [LCImageChoose shareCameraWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picTwo.image = data[0];
            self.hd_report_pic_two = [UIImageJPEGRepresentation(self.img_picTwo.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            
            self.img_picTwoDel.image = [UIImage imageNamed:@"del"];
        }];
    }
   
}

- (IBAction)clickDelPicTwo:(id)sender {
    self.img_picTwoDel.image = [UIImage imageNamed:@""];
    self.img_picTwo.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_two = @"";
   
}


- (IBAction)clickPicThree:(id)sender {
    [self.view endEditing:YES];
    
    if ([self haveStr:@[self.hd_report_pic_three]]) {
        [SQShowImage showImage:self.img_picThree];
        
    }else{
        [LCImageChoose shareCameraWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_picThree.image = data[0];
            self.hd_report_pic_three = [UIImageJPEGRepresentation(self.img_picThree.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_picThreeDel.image = [UIImage imageNamed:@"del"];
        }];
    }
    
    
}

- (IBAction)clickDelPicThree:(id)sender {
    self.img_picThreeDel.image = [UIImage imageNamed:@""];
   self.img_picThree.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_three = @"";
}


@end
